﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:fn="http://www.w3.org/2005/xpath-functions"
                xmlns:rdm="urn:rdm-data"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                exclude-result-prefixes="xs fn rdm msxsl">
        <xsl:output method="xml"
                    version="1.0"
                    encoding="utf-8"
                    indent="yes"
                />
    <xsl:strip-space elements="*" />
    <!--<msxsl:script implements-prefix="rdm" language="CSharp">
        
        
    </msxsl:script>-->

    <!--<xsl:param name="ServiceName" select="'Service'" />
    <xsl:param name="SolutionFolder" select="'..\..\AcceleratorService'" />-->
        <!--<xsl:choose>
            <xsl:when test="(function-available('fn:environment-variable')) and boolean(fn:environment-variable('_acceleratorSolution'))">
                <xsl:value-of select="string(fn:environment-variable('_acceleratorSolution'))"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="string('..\\..\\..\\..\\AcceleratorService')"/>
            </xsl:otherwise>
        </xsl:choose>--><!-- 
    <xsl:variable name="accfgFile" select="concat($SolutionFolder, '\\', $ServiceName, 'Accelerator.accfg')" />
    <xsl:variable name="cscfgFile" select="concat($SolutionFolder, '\\', $ServiceName, 'Configuration.cscfg')" />
    <xsl:variable name="csdefFile" select="concat($SolutionFolder, '\\', $ServiceName, 'Definition.csdef')" />
    <xsl:variable name="cscfg" select="document($cscfgFile)" />
    <xsl:variable name="csdef" select="document($csdefFile)" />
    <xsl:variable name="accfg" select="/*/*" />-->


    <!--<xsl:template match="/">

        <xsl:param name="overrideFile" />
        <xsl:call-template name="merge">
            <xsl:with-param name="std" select="."/>
            <xsl:with-param name="ovrd" select="document($overrideFile)"/>
        </xsl:call-template>
    </xsl:template>-->
    <xsl:template name="merge">
        <xsl:param name="std"/>
        <xsl:param name="ovrd"/>
        <xsl:for-each select="$std/*">
            <xsl:variable name="key" select="@*[1]"/>
            <xsl:variable name="ovr"
               select="$ovrd/*[local-name() = local-name(current()) 
                 and (not($key) or @*[1] = $key)]"/>
            <xsl:if test="count($ovr) = 0">
                <xsl:copy-of select="."/>
            </xsl:if>
            <xsl:if test="count($ovr) = 1 and (not($ovr/@overrideMode) 
                    or $ovr/@overrideMode != 'delete')">
                <xsl:choose>
                    <xsl:when test="count($ovr/*) = 0 or $ovr/@overrideMode = 'update' 
                          or $ovr/@overrideMode = 'replace'">
                        <xsl:variable name="current" select="."/>
                        <xsl:for-each select="$ovr">
                            <xsl:copy>
                                <xsl:for-each select="@*[name() != 'overrideMode']|
                                      text()[string-length(normalize-space(.))>0]">
                                    <xsl:copy/>
                                </xsl:for-each>
                                <xsl:choose>
                                    <xsl:when test="$ovr/@overrideMode = 'replace'">
                                        <xsl:copy-of select="*"/>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:call-template name="merge">
                                            <xsl:with-param name="std" select="$current"/>
                                            <xsl:with-param name="ovrd" select="."/>
                                        </xsl:call-template>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:copy>
                        </xsl:for-each>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:copy>
                            <xsl:for-each select="@*|text()[string-length(normalize-space(.))>0]">
                                <xsl:copy/>
                            </xsl:for-each>
                            <xsl:call-template name="merge">
                                <xsl:with-param name="std" select="."/>
                                <xsl:with-param name="ovrd" select="$ovr"/>
                            </xsl:call-template>
                        </xsl:copy>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:if>
        </xsl:for-each>
        <xsl:for-each select="$ovrd/*">
            <xsl:variable name="key" select="@*[1]"/>
            <xsl:if test="count($std/*[local-name() = local-name(current()) 
                    and (not($key) or @*[1] = $key)]) = 0">
                <xsl:copy-of select="."/>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>
